/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:53 AM
------------------------------------------------------*/
using hou_elearning.DataAccess.Base;
using hou_elearning.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace hou_elearning.DataAccess
{    
    public class LichhocDAL : SqlProvider<LichhocEntity>
    {
        static LichhocDAL()
        {
            InitReader();
        }
        protected static void InitReader()
        {
            getFromReader=delegate(SqlDataReader dr)
            {
                LichhocEntity entity = new LichhocEntity();
				entity.PK_iLichhocID = Int64.Parse("0"+dr["PK_iLichhocID"].ToString());
				entity.FK_iKhoahoc_BaihocID = Int64.Parse("0"+dr["FK_iKhoahoc_BaihocID"].ToString());
				entity.FK_iNguoitao = Int64.Parse("0"+dr["FK_iNguoitao"].ToString());
				entity.dNgaytao =String.IsNullOrEmpty(dr["dNgaytao"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaytao"].ToString());
				entity.FK_iNguoicapnhat = Int64.Parse("0"+dr["FK_iNguoicapnhat"].ToString());
				entity.dNgaycapnhat =String.IsNullOrEmpty(dr["dNgaycapnhat"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaycapnhat"].ToString());
				entity.dNgayhoc =String.IsNullOrEmpty(dr["dNgayhoc"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgayhoc"].ToString());
				entity.dGiobatdau =String.IsNullOrEmpty(dr["dGiobatdau"].ToString())?DateTime.Now:DateTime.Parse(dr["dGiobatdau"].ToString());
				entity.dGioketthuc =String.IsNullOrEmpty(dr["dGioketthuc"].ToString())?DateTime.Now:DateTime.Parse(dr["dGioketthuc"].ToString());
				entity.sGhichu = dr["sGhichu"].ToString();
				entity.iTrangthai = Int16.Parse("0"+dr["iTrangthai"].ToString());
                return entity;
            };
        }
        public static LichhocEntity GetOne(Int64 PK_iLichhocID)
        {
            string cmdName = "spLichhoc_GetByPK";
            SqlParameter p = new SqlParameter("@PK_iLichhocID", PK_iLichhocID);
            LichhocEntity entity = GetOne(cmdName, p);
            return entity;
        }
        public static List<LichhocEntity> GetAll()
        {
            string cmdName = "spLichhoc_Get";
            return GetList(cmdName);
        }
        public static List<LichhocEntity> GetByFK_iKhoahoc_BaihocID(Int64 FK_iKhoahoc_BaihocID)
		{
			string cmdName = "spLichhoc_GetByFK_FK_iKhoahoc_BaihocID";
			SqlParameter p = new SqlParameter("@FK_iKhoahoc_BaihocID",FK_iKhoahoc_BaihocID);
			List<LichhocEntity> list = GetList(cmdName, p);
			return list;
		}
        public static List<LichhocEntity> GetByFK_iKhoahocID(Int64 FK_iKhoahocID)
        {
            string cmdName = "spLichhoc_GetByFK_FK_iKhoahocID";
            SqlParameter p = new SqlParameter("@FK_iKhoahocID", FK_iKhoahocID);
            List<LichhocEntity> list = GetList(cmdName, p);
            return list;
        }
        public static List<LichhocEntity> GetByFK_iNguoitao(Int64 FK_iNguoitao)
		{
			string cmdName = "spLichhoc_GetByFK_FK_iNguoitao";
			SqlParameter p = new SqlParameter("@FK_iNguoitao",FK_iNguoitao);
			List<LichhocEntity> list = GetList(cmdName, p);
			return list;
		}public static List<LichhocEntity> GetByFK_iNguoicapnhat(Int64 FK_iNguoicapnhat)
		{
			string cmdName = "spLichhoc_GetByFK_FK_iNguoicapnhat";
			SqlParameter p = new SqlParameter("@FK_iNguoicapnhat",FK_iNguoicapnhat);
			List<LichhocEntity> list = GetList(cmdName, p);
			return list;
		}
        public static int Add(LichhocEntity entity)
        {
            string cmdName = "spLichhoc_Insert";
            return Run(cmdName,true,initParams(entity));

        }
        public static bool Edit(LichhocEntity entity)
        {
            string cmdName = "spLichhoc_Update";
            return Run(cmdName,false,initParams(entity))>0;
        }
        public static bool Remove(Int64 PK_iLichhocID)
        {
            string cmdName = "spLichhoc_Delete";
            SqlParameter p = new SqlParameter("@PK_iLichhocID", PK_iLichhocID);
            return Run(cmdName,false,p)>0;
        }
        #region private
        private static SqlParameter[] initParams(LichhocEntity entity)
        {
            SqlParameter[] p = new SqlParameter[11];
			p[0] = new SqlParameter("@PK_iLichhocID", entity.PK_iLichhocID);
			p[1] = new SqlParameter("@FK_iKhoahoc_BaihocID", entity.FK_iKhoahoc_BaihocID);
			p[2] = new SqlParameter("@FK_iNguoitao", entity.FK_iNguoitao);
			p[3] = new SqlParameter("@dNgaytao", entity.dNgaytao);
			p[4] = new SqlParameter("@FK_iNguoicapnhat", entity.FK_iNguoicapnhat);
			p[5] = new SqlParameter("@dNgaycapnhat", entity.dNgaycapnhat);
			p[6] = new SqlParameter("@dNgayhoc", entity.dNgayhoc);
			p[7] = new SqlParameter("@dGiobatdau", entity.dGiobatdau);
			p[8] = new SqlParameter("@dGioketthuc", entity.dGioketthuc);
			p[9] = new SqlParameter("@sGhichu", entity.sGhichu);
			p[10] = new SqlParameter("@iTrangthai", entity.iTrangthai);
            return p;
        }
        #endregion
       
    }
}